package com.erps.config;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.erps.CommonUtils.ActionResult;
import com.erps.CommonUtils.AppConfig;
import com.erps.CommonUtils.DateUtils;
import com.erps.CommonUtils.Sequence;
import com.erps.CommonUtils.StringEx;
import com.erps.CommonUtils.ValueType;
import com.erps.model.ResRequestModel;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;

public class SaveResRequestController extends Controller {
	public void save() {
		// String res_typeid=StringEx.getStr(getPara("res_typeid"));
		String user_id = StringEx.getStr(getSessionAttr(AppConfig.SESSION_USERID));
		String org_id = StringEx.getStr(getSessionAttr(AppConfig.SESSION_ORGID));
		String res_id = StringEx.getStr(getPara("res_id"));
		String req_content = StringEx.getStr(getPara("req_content"));

		long req_start = StringEx.getLong(DateUtils.getDayTimeNum(getPara("req_start") + " 00:00:00"));
		long req_end = StringEx.getLong(DateUtils.getDayTimeNum(getPara("req_end") + " 00:00:00"));

		// if(!res_typeid.equals(11)||!res_typeid.equals("13")){//判断资源是否需要预约时间限制
		// doSave(user_id,org_id,res_id,req_start,req_end,req_content);
		// }else{
		List<ResRequestModel> list = isRequested(user_id, res_id, req_start, req_end);// 判断资源在某个时间范围内是否被预约过
		if (list.size() > 0) {
			setAttr("res_list", list);
			ActionResult ret = ActionResult.Valid("已被其他用户占用！");
			renderJson(JSON.toJSONString(ret));
		} else {
			doSave(user_id, org_id, res_id, req_start, req_end, req_content);
		}
		// }
	}

	public List<ResRequestModel> isRequested(String user_id, String res_id, long req_start, long req_end) {
		ResRequestModel request = getModel(ResRequestModel.class);
		StringBuilder sql = new StringBuilder();
		sql.append("select * from ersp_res_request ");
		sql.append(" where user_id=" + user_id + " and res_id=" + res_id);
		sql.append(" and (req_start>" + req_start + " and req_start<" + req_end + ")");
		sql.append(" or (req_start<" + req_start + " and req_end>" + req_end + ")");
		sql.append(" or (req_end>" + req_start + " and req_end>" + req_end + ")");
		List<ResRequestModel> list = request.find(sql.toString());

		// request.find(sql);
		return list;
	}

	public void doSave(String user_id, String org_id, String res_id, long req_start, long req_end, String req_content) {
		ResRequestModel request = getModel(ResRequestModel.class);
		ActionResult ret = ActionResult.Valid(ValueType.FAILURE);
		long last_update_time = DateUtils.getDayTimeNum();
		try {
			request.set("id", Sequence.getGuid());
			request.set("user_id", user_id);
			request.set("org_id", org_id);
			request.set("res_id", res_id);
			request.set("req_start", req_start);
			request.set("req_end", req_end);
			request.set("req_content", req_content);
			request.set("last_update_time", last_update_time);
			request.set("create_time", last_update_time);
			request.save();
			ret = ActionResult.Valid(ValueType.SUCCESS);
		} catch (Exception ex) {

		}
		renderJson(JsonKit.toJson(ret));
	}

}
